{extend name="base"/}

{block name="resources"}
<style type="text/css">
	/*.js-domain{*/
		/*display: none;*/
	/*}*/
	.refresh-time {
		color: #B2B2B2;
		margin-top: 10px;
	}
</style>
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>为满足不同用户的需求，方便快速搭建手机端，增加以下三种部署方式供其选择，易上手难度递增。</li>
			<li>（难度：简单）默认部署：无需下载，一键刷新，API接口请求地址默认为当前域名，编译代码存放到h5文件夹中。</li>
			<li>（难度：中等）独立部署：下载编译代码包后，放到网站根目录下运行。</li>
			<li>（难度：较高）独立部署：下载uniapp代码包，可进行二次开发。</li>
		</ul>
	</div>
</div>
<div class="layui-form ns-form">

	<div class="layui-form-item">
		<label class="layui-form-label">部署方式：</label>
		<div class="layui-input-block">
			<input type="radio" name="deploy_way" value="default" lay-filter="deploy_way" title="默认部署" data-desc="无需下载，一键刷新，API接口请求地址为当前域名，编译代码存放到h5文件夹中" {if $config.deploy_way eq 'default'}checked{/if} />
			<input type="radio" name="deploy_way" value="indep" lay-filter="deploy_way" title="独立部署" data-desc="可下载编译包或源码包进行独立部署" {if $config.deploy_way eq 'indep'}checked{/if}/>
		</div>
		<div class="ns-word-aux js-desc">
            {if $config.deploy_way eq 'default'}
                无需下载，一键刷新，API接口请求地址为当前域名，编译代码存放到h5文件夹中
            {else/}
                可下载编译包或源码包进行独立部署
            {/if}      
        </div>
	</div>

    <div class="deploy-way default {if $config.deploy_way neq 'default'}layui-hide{/if}">
        <div class="layui-form-item web-url">
            <label class="layui-form-label">手机端域名：</label>
            <div class="layui-input-block">
                <a href="{$root_url}/h5" target="_blank" class="ns-text-color">{$root_url}/h5</a>
            </div>
        </div>
        <div class="ns-form-row" style="margin-top: 0;">
            <button class="layui-btn ns-bg-color js-save" onclick="refreshh5()">刷新</button>
            {if $refresh_time}<p class="refresh-time">上次刷新时间：{:time_to_date($refresh_time)}</p>{/if}
        </div>
    </div>
	
    <div class="deploy-way indep {if $config.deploy_way neq 'indep'}layui-hide{/if}">
        <div class="layui-form-item">
            <label class="layui-form-label">手机端域名：</label>
            <div class="layui-input-block">
                <input type="text" name="domain" lay-verify="domain" {if $config.deploy_way eq 'indep'}value="{$config['domain_name_h5'] ?? ''}"{/if} autocomplete="off" class="layui-input ns-len-long">
            </div>
            <div class="ns-word-aux">域名末尾无需拼接“/”</div>
        </div>
        <div class="layui-form-item web-url">
            <label class="layui-form-label">源码下载：</label>
            <div class="layui-input-block">
                <button class="layui-btn ns-bg-color" lay-submit lay-filter="downloadindep">H5编译包下载</button>
                {if $is_auth}
                <button class="layui-btn ns-bg-color" onclick="window.open(ns.url('admin/h5/downloaduniapp'));">UNIAPP源码包下载</button>
                {/if}
            </div>
            <div class="ns-word-aux">H5编译包下载之后直接解压到手机端域名根目录即部署完成。</div>
            <div class="ns-word-aux">UNIAPP源码包下载之后可进行二次开发，可自行发行H5进行部署。</div>
        </div>
    </div>
   
    <div class="ns-form-row">
        <button class="layui-btn ns-bg-color js-save" lay-submit lay-filter="save">保存</button>
    </div>

</div>
{/block}

{block name="script"}
<script type="text/javascript">
    layui.use(['form'], function() {
        var form = layui.form,
            repeat_flag = false; //防重复标识;
        $(".js-desc").text($("input[name='deploy_way']:checked").attr("data-desc"));

        form.on('radio(deploy_way)', function(data){
            var value = $(data.elem).val();
            $('.deploy-way').addClass('layui-hide');
            $('.deploy-way.' + value).removeClass('layui-hide');

            var desc = $(data.elem).attr("data-desc");
            $(".js-desc").text(desc);
        })

        form.verify({
            domain : function(value, item) {
                var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/; //正则表达式验证域名
                if($("input[name='deploy_way']:checked").val() == "indep") {
                    if (value === '') {
                        return "请输入域名地址";
                    } else if (!(reg.test(value))) {
                        return '请输入正确的域名地址';
                    }
                }
            }
        });

        form.on("submit(save)",function(data){
            if (repeat_flag) return false;
            repeat_flag = true;
            $.ajax({
                url: ns.url("admin/h5/h5domainname"),
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(res){
                    repeat_flag = false;
                    layer.msg(res.message);
                }
            });
        })


        form.on("submit(downloadindep)",function(data){
            window.open(ns.url('admin/h5/downloadindep', {domain: data.field.domain}));
        })    

    });

    function refreshh5(){
        if (repeat_flag) return false;
        repeat_flag = true;
        $.ajax({
            url: ns.url("admin/h5/refreshh5"),
            dataType: 'JSON',
            type: 'POST',
            success: function(res){
                repeat_flag = false;
                layer.msg(res.message);
            }
        });
    }
</script>
{/block}